﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ClassLibrary.Data;
using System.Data;
using ClassLibrary.Entity;

namespace ClassLibrary.DAO
{
    public class TblItemsDAO
    {
        private DBService _db = null;

        public TblItemsDAO(String connectionString)
        {
            _db = new DBService(connectionString);
        }

        public bool InsUpdItem(TblItems Item, int mode)
        {
            bool flag = false;
            try
            {
                _db.ExeSP_NonQuery("sp_TblItems_InsUp",
                    new string[] { "@IDItem", "@ItemName", "@ItemNumber", "@ItemDescription", "@Features", "@Specification", "@IDMenuChild", "@IDSupplie", "@Price", "@Mode" },
                    new object[] { Item.IDItem, Item.ItemName, Item.ItemNumber, Item.ItemDescription, Item.Features, Item.Specification, Item.IDMenuChild, Item.IDSupplie, Item.Price, mode },
                    CommandType.StoredProcedure);
                flag = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return flag;
        }

        public bool InsImageItem(TblItemImage imgItem, int mode)
        {
            bool flag = false;
            try
            {
                _db.ExeSP_NonQuery("sp_TblItemImage_Ins",
                    new string[] { "@p_IDItem", "@p_ImageURL" },
                    new object[] { imgItem.IDItem, imgItem.ImageURL },
                    CommandType.StoredProcedure);
                flag = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }

        public DataTable GetImgItems(int IDItem)
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("select * from TblItemImage where IDItem = @IDItem",
                    new string[] { "@IDItem" },
                    new object[] { IDItem },
                    CommandType.Text);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        public DataTable GetItemsAll()
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("select vItems.* from vItems",
                    false, CommandType.Text);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }
        public DataTable GetSPNoiBat()
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("select top 3 vItems.* from vItems",
                    false, CommandType.Text);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }
    
        public TblItems GetItemByID(int IDItem)
        {
            TblItems item = new TblItems();
            try
            {
                DataTable dt = _db.ExeSP_DataTable("select vItems.* from vItems " +
                                        " where vItems.IDItem = @IDItem ",
                    new string[] { "@IDItem" },
                    new object[] { IDItem }
                    , CommandType.Text);
                foreach (DataRow dr in dt.Rows)
                {
                    item.IDItem = IDItem;
                   
                    item.ItemName = dr["ItemName"].ToString();
                    item.ItemNumber = dr["ItemNumber"].ToString();
                    item.IDMenuChild = Convert.ToInt32(dr["IDMenuChild"]);
                    item.IDSupplie = Convert.ToInt32(dr["IDSupplie"]);
                    item.Price = Convert.ToDecimal(dr["Price"]);
                    item.Features = dr["Features"].ToString();
                    item.Specification = dr["Specification"].ToString();
                    item.ItemDescription = dr["ItemDescription"].ToString();   
  
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return item;
        }

        //get item by idchild
        public DataTable GetItemByIDChild(int IDMenuChild)
        {
            DataTable dt = null;
            try
            {
                 dt = _db.ExeSP_DataTable("select vItems.* from vItems " +
                                        " where vItems.IDMenuChild = @IDMenuChild ",
                    new string[] { "@IDMenuChild" },
                    new object[] { IDMenuChild }
                    , CommandType.Text);
               
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        // GetAll Item
        public DataTable GetItemByIDItem(int IDItem)
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("select vItems.* from vItems " +
                                       " where vItems.IDItem = @IDItem ",
                   new string[] { "@IDItem" },
                   new object[] { IDItem }
                   , CommandType.Text);

            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        // tim kiem sp

        public DataTable Search(string ItemName, string Suppliers, int Mode)
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("sp_TblItemsList",
                   new string[] { "@ItemName", "@Suppliers", "@Mode" },
                   new object[] { ItemName, Suppliers, Mode }
                   , CommandType.StoredProcedure);

            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        public bool DeleteItem(int IDItem)
        {
            bool flag = false;
            try
            {
                _db.ExeSP_NonQuery("delete from TblItems where IDItem = @IDItem",
                    new string[] { "@ItemID" },
                    new object[] { IDItem },
                    CommandType.Text);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }

        public bool DeleteImgItem(TblItemImage imgItes)
        {
            bool flag = false;
            try
            {
                _db.ExeSP_NonQuery("sp_TblItemImage_Del",
                    new string[] { "@p_ID" },
                    new object[] { imgItes.ID },
                    CommandType.StoredProcedure);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }
    }

}
